package com.topcoder.srm523;

public class CountingSeries {

	public long countThem(long a, long b, long c, long d, long upperBound) {
		long count = 0;
		long z = c;
		for (long y=0; z<=upperBound; y++) {
			if (z<a || (z-a)%b!=0) count++;
			z *= d;
			if (d == 1) break;
		}
		
		long count2 = upperBound>=a? (upperBound - a)/b + 1 : 0;
		
		return count + count2;
	}

}
